<?php

namespace App\Http\Controllers\Sale;

use App\Http\Controllers\Controller;
use App\Models\SaleUser;
use App\Models\SaleUserLogin;
use App\Models\SaleLog;
use App\Models\ShopSms;
use App\Services\CommonService;
use App\Services\SmsService;
use Illuminate\Http\Request;

//供应商入驻
class SaleApplyController extends Controller
{
    //添加分销商POST /Sale/SaleApply/addUser
    public function addUser(Request $request)
    {
        $input = $request->input();

        // if (empty($input['contact_tel']) || empty($input['email']) || empty($input['faname']) || empty($input['idcard']) || empty($input['faidcard']) || empty($input['faidcard_back']) || empty($input['code']) || empty($input['regustCode'])) {
        if (empty($input['contact_tel']) || empty($input['email']) || empty($input['faname']) || empty($input['idcard']) || empty($input['password']) || empty($input['regustCode'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
//        $checkCode = SmsService::checkCode($input['contact_tel'],$input['regustCode']);
//         if(!$checkCode){
//             return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码错误');
//         }
        if (!Verify_Phone($input['contact_tel'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
        } else {
            $saleUser = SaleUserLogin::GetSaleUser(array('mobile' => $input['contact_tel']));
            if ($saleUser) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该手机号码已注册，请直接登录');
            }
        }
//        if($input['companyid']){
//            if (!isBusinessRegistrationNumber($input['companyid'])) {
//                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效工商注册号');
//            }
//        }
//        if (!check_passportid($input['idcard'])) {
//            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效身份证号');
//        }
        if (!check_email($input['email'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '邮箱格式不正确');
        }

        if(strlen($input['password'])<6){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '密码不能小于6位');
        }

        if(!isset($input['code'])||empty($input['code'])){
            $input['code']='000001';
        }
        $code = SaleUser::GetSaleUser(array('code' => $input['code']));
        if($code || $input['code'] == 'TXXZ'){
            if($input['code'] == 'TXXZ'){
                $level = 1; //总级别
                $code = 0;
            }else if($code['parent_user_id'] == 0){
                $level = 2; //一级分销商
                $code = $code['sale_user_id'];
            }else{
                $level = 3; //二级分销商
                $code = $code['sale_user_id'];
            }
            $sale_user_id = date('mdHis').mt_rand(10,99);
           if(empty($code)&& $input['code'] != 'TXXZ'){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '邀请码不存在');
           }
            $addData = array(
                'sale_user_id'=>$sale_user_id,//经销商ID
                'parent_user_id' => $code, //父级ID
                'people' => $input['people']?$input['people']:$input['faname'], //联系人
                'contact_tel' => $input['contact_tel'], //联系人手机号
                'user_type' => $input['user_type'], //1企业/2个人
                'level' => $level, //分销商级别
                'area_id' => $input['area_id'] ? $input['area_id']:0, //区域城市ID
                'address' => $input['address'] ? $input['address']:'', //公司地址
                'companyid' => $input['companyid']?$input['companyid']:'', //工商/营业执照注册号
                'mechanism' => $input['mechanism']?$input['mechanism']:'', //公司/机构名称
                'faname' => $input['faname'], //法人/个人姓名
                'idcard' => $input['idcard'], //法人/个人身份证号
                //'faidcard' => $input['faidcard'], //法人/个人身份证正面
                //'faidcard_back' => $input['faidcard_back'], //法人/个人身份证反面(带国徽的一面)
                //'shopcard' => $input['shopcard']?$input['shopcard']:'',, //营业执照照片
                'shopcity' => $input['shopcity']?$input['shopcity']:'全国', //当前经营位置 省-市
                'create_time'=>date('Y-m-d H:i:s'), // 入驻时间
                'update_time'=>date('Y-m-d H:i:s'), // 修改时间
                'is_show' => CommonService::SHOP_ENABLE, //是否启用 1启用SHOP_ENABLE2不启用SHOP_DISABLE
                'email' => $input['email'], //邮箱
                'code' => invitation_no(),//邀请码
                'is_check' => CommonService::SHOP_ENABLE, //是否审核1审核2待审核
                'end_time' =>date('Y-m-d H:i:s',time()+365*24*3600), //失效时间365天后
                'balance'=>0, // 账户余额
                'balance_end'=>0, // 账户冻结余额
                'type'=>1, //1线上2线下
                'balance_drawal'=>0, // 正在提现金额-冻结金额
            );
            //添加经销商
            $id = SaleUser::insertGetId($addData);
            if ($id) {
                //添加账号
                $userData = array(
                        'sale_user_id'=>$sale_user_id,
                        'username'=>$input['people']?$input['people']:$input['faname'], //用户名/昵称
                        'password'=>$input['password']?md5($input['contact_tel'].$input['password']):md5($input['contact_tel'].'dlz'.$input['contact_tel']), // 密码
                        'mobile'=>$input['contact_tel'], //手机号码
                        'is_show'=>CommonService::SHOP_ENABLE, //是否启用1启用2不启用
                        'role'=>CommonService::SALE_ALL,//角色 1总账号2子账号
                        'create_time'=>date('Y-m-d H:i:s'),//创建时间

                    );
                $res = SaleUserLogin::insertGetId($userData);
                if($res){
                    //添加操作记录
                    $data = [];
                    $data['sale_user_id'] = $sale_user_id;
                    $data['user_id'] = $res;
                    $data['content'] = '分销商'.$input['people']?$input['people']:$input['faname'].'注册账号';
                    $data['remark'] = '';
                    SaleLog::addLog($data);

                    //发短信
                    // ShopSms::saleRegister($input['people'],$input['contact_tel']);

                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '提交成功，请等待平台审核');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '提交失败，请稍后再试或联系平台客服.');
                }

            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '提交失败，请稍后再试或联系平台客服');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入正确的邀请码');
        }

    }


}
